public class UsePostorderAndInorderCreateTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public int postorderInder;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        postorderInder = postorder.length - 1;//最后一个数据的下标
        return buildTreeChild(inorder,postorder,0,inorder.length - 1);
    }

    public TreeNode buildTreeChild(int[] inorder,int[] postorder,int ib,int ie) {
        if (ib > ie) {
            return null;
        }

        TreeNode root = new TreeNode(postorder[postorderInder]);

        int rootIndex = findval(inorder,postorder[postorderInder],ib,ie);

        postorderInder--;

        root.right = buildTreeChild(inorder,postorder,rootIndex + 1,ie);

        root.left = buildTreeChild(inorder,postorder,ib,rootIndex - 1);

        return root;
    }

    public int findval(int[] inorder, int val, int ib,int ie) {

        for (int i = ib; i <= ie; i++) {
            if (inorder[i] == val) {
                return i;
            }
        }
        return -1;
    }
}
